//
//  addCustomerViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/12.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddCustomerViewController.h"
#import "ResizeView.h"
#import "HttpCustomerAction.h"

@interface AddCustomerViewController (){
    AddCustomerBlock addCustomerBlock;
}
@property (nonatomic,strong) UITextField *userNameTF;
@property (nonatomic,strong) UITextField *passwordTF;
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UITextField *telTF;
@property (nonatomic,strong) UITextField *cardTF;

@end

@implementation AddCustomerViewController

-(id)initWithBlock:(AddCustomerBlock)block{
    if (self == [super init]) {
        addCustomerBlock = block;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加客户";
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:20]];
    ResizeViewInfo *userNameRV = [self getNormalResizeViewInfo:@"用户名:"];
    self.userNameTF = (UITextField*)[userNameRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:userNameRV];
    ResizeViewInfo *passwordRV = [self getNormalResizeViewInfo:@"密码:"];
    self.passwordTF = (UITextField*)[passwordRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:passwordRV];

    ResizeViewInfo *nameRV = [self getNormalResizeViewInfo:@"姓名:"];
    self.nameTF = (UITextField*)[nameRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:nameRV];
    ResizeViewInfo *telRV = [self getNormalResizeViewInfo:@"电话:"];
    self.telTF = (UITextField*)[telRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:telRV];
    ResizeViewInfo *cardRV = [self getNormalResizeViewInfo:@"身份证:"];
    self.cardTF = (UITextField*)[cardRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:cardRV];
    
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.userNameTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入用户名" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.passwordTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入密码" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.nameTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入姓名" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.telTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入电话" autoDissmissAfterDelay:0];
        return;
    }if ([self.cardTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入身份证" autoDissmissAfterDelay:0];
        return;
    }
    CustomerActionInfo *customerActionInfo = [[CustomerActionInfo alloc] init];
    customerActionInfo.realName = self.nameTF.text;
    customerActionInfo.phone = self.telTF.text;
    customerActionInfo.idCard = self.cardTF.text;
    customerActionInfo.type = @"5";
    customerActionInfo.name = self.userNameTF.text;
    customerActionInfo.password = self.passwordTF.text;
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpCustomerAction postAddCustomer:customerActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            if (addCustomerBlock) {
                addCustomerBlock();
            }
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
    }];
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    return resizeViewInfo;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
